Mathematical Functions
- Typing help elfun and help specfun calls up full lists of elementary and special functions respectively.
- There is a long list of mathematical functions that are built into MATLAB that are called built-ins.
- Many standard mathematical functions, such as sin(x), cos(x), tan(x), ex, ln(x), are evaluated by the functions sin, cos, tan, exp, and log respectively in MATLAB.
- Listed are some commonly used functions, where variables x and y can be numbers, vectors, or matrices.
- Note: only use built-in functions on the right hand side of an expression.
cos(x) | Cosine |
sin(x) | Sine |
tan(x) | Tangent |
acos(x) | Arc cosine |
asin(x) | Arc sine |
atan(x) | Arc tangent |
exp(x) | Exponential |
sqrt(x) | Square root |
log(x) | Natural logarithm |
log10(x) | Common logarithm |
abs(x) | Absolute value |
sign(x) | Signum function |
max(x) | Maximum value |
min(x) | Minimum value |
ceil(x) | Round towards +∞ |
floor(x) | Round towards -∞ |
round(x) | Round to the nearest integer |
rem(x) | Remainder after division |
angle(x) | Phase angle |
conj(x) | Complex conjugate |
pi | The number Π = 3.14159... |
i,j | The imaginary unit i, |
Inf | The infinity |
NaN | Not A Number |
Question 1
Determine the value of the expression , for a = 5, x = 2, and y = 8 using MATLAB.
Graphs
- MATLAB has an excellent set of graphic tools.
- Plotting a given data set or the results of computation is possible with very few commands.
- Being able to plot mathematical functions and data freely is a very important step.
- The basic 2D MATLAB graphing procedure is to take a vector of:
- x-coordinates: x = (x1; : : : ; xN), and
- y-coordinates, y = (y1; : : : ; yN),
- locate the points (xi; yi), with i = 1; 2; : : : ; n
- and then join them by straight lines.
- As programs grow in complexity, it is important to insert comments
- Comments are preceded by a % symbol
- First, x and y need to be set in an identical array form; namely, x and y are both row arrays or column arrays of the same
- The MATLAB command to plot a graph is plot(x,y).
- The vectors x = (1; 2; 3; 4; 5; 6) and y = (0.5; 1; 5; 2; 2.5; 3) can be plotted as.
- x = [1 2 3 4 5 6];
- y = [0.5 1 1.5 2 2.5 3];
- plot(x,y)
- To plot the function sin (x) on the interval [0; π], first create a vector of x values ranging from 0 to 2π with steps of π/100, then compute the sin of these values, and finally plot the result:
- %This program plots a sin function between 0 and 2π
- x = 0:pi/100:2*pi; % Set x array in steps of π/100
- y = sin(x); % Set the y array
- plot(x,y) % Generate the graph
Axis Labels and Title
- MATLAB enables axis labels and titles to be added
- For example, using the graph from the previous example, add an x- and y-axis labels:
- %This program plots a sin function between 0 and 2π
- x = 0:pi/100:2*pi; % Set x array in steps of π/100
- y = sin(x); % Set the y array
- plot(x,y) % Generate the graph
- The character \pi creates the symbol π
- The graph axis titles, labels and appearance can be changes with the menu bar at the top of the graph e.g. Edit – Axes Properties or Insert – X-Label, Y-Label
Multiple Data Sets
- Multiple (x; y) pairs arguments create multiple graphs with a single call to plot.
- For example, plot three related functions of x: y1 = 2 cos(x), y2 = cos(x), and y3 = 0:5cos(x), in the interval 0 < x < 2π:
- x = 0:pi/100:2*pi; % Set the x array of values
- y1 = 2*cos(x); % Set the first function
- y2 = cos(x); % Set the second function
- y3 = 0.5*cos(x); % Set the third function
- plot(x,y1,x,y2,x,y3) % Plot the three functions
Question 2
Plot a graph of the following harmonics of a power signal as a function of x:
- y1 = 100sin(x),
- y2 = 30sin(3x), and
- y1 + y2
in the interval 0 < x < 2π using MATLAB.
Question 3
Use MATLAB to plot the function:
- S = 2 sin(3t + 2) +
over the interval 0 ≤ t ≤ 5. Put a title on the plot, and properly label the axes using the Edit, Axis Properties on the graph.
Variables
- the variable s represents speed in metres per second;
- the variable t represents time in seconds, use a step value of 0.1.
Question 4
Use MATLAB to plot the functions y = 4 and z = 5e0.3x – 2x over the interval 0 ≤ x ≤ 1.5 in steps of 0.1. Properly label the plot and each curve.
The variables y and z represent force in Newtons; the variable x represents distance in meters.